package de.tu_darmstadt.gdi1.xcom.tests.testcases.tutors;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

import de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel1;

/**
 * 
 * Tests if movements and rotations are performed correctly
 * 
 * @author Lukas Schwoebel
 *
 */
public class MovementTest {
	
	private XcomTestAdapterLevel1 testAdapter;
	
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		testAdapter = new XcomTestAdapterLevel1();
	}
	
	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel1#rotateActivePlayer()}.
	 */
	@Test
	public final void testDoRotations() {
		testAdapter.loadLevelFromString(TestLevel.testLevel);

		// just do some "random" rotations with "random" players
		
		testAdapter.changeActivePlayer(11, 4);
		assertEquals("Player should look north", 1, testAdapter.activePlayerLookingToDirection());
		testAdapter.rotateActivePlayer(false);
		assertEquals("Player should look west", 4, testAdapter.activePlayerLookingToDirection());
		testAdapter.rotateActivePlayer(true);
		testAdapter.rotateActivePlayer(true);
		assertEquals("Player should look east", 2, testAdapter.activePlayerLookingToDirection());
		
		// now change player
		testAdapter.changeActivePlayer(4, 3);
		testAdapter.rotateActivePlayer(true);
		testAdapter.rotateActivePlayer(true);
		assertEquals("Player should look south", 3, testAdapter.activePlayerLookingToDirection());
		
		// change back to "first" player, still look east
		testAdapter.changeActivePlayer(11, 4);
		assertEquals("Player should look east", 2, testAdapter.activePlayerLookingToDirection());	
	}

	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel1#performActionForActivePlayer()}.
	 */
	@Test
	public final void testWalkingActionValid() {
		testAdapter.loadLevelFromString(TestLevel.testLevel);
		
		// try to walk into another player
		testAdapter.changeActivePlayer(9, 2);
		testAdapter.rotateActivePlayer(true);
		testAdapter.performActionForActivePlayer();
		testAdapter.performActionForActivePlayer();
		testAdapter.rotateActivePlayer(true);
		testAdapter.performActionForActivePlayer();
		// now two player are facing each other
		assertEquals("Player should have 64 actionpoints after 2 rotations and 3 movements", 64, testAdapter.actionPointsLeft());
		testAdapter.performActionForActivePlayer();
		assertEquals("Movementaction was invalid, not possible to walk into another player, still 64 actionpoints", 64, testAdapter.actionPointsLeft());
		// check if player at 11:4 still exists
		testAdapter.changeActivePlayer(11, 4);
		assertEquals("Player at 11:4 should still exist and should have 75 actionpoints", 75, testAdapter.actionPointsLeft());
		assertEquals("Player at 11:3 should still be there", '@', testAdapter.elementOnPosition(11, 3));
		assertEquals("Player at 11:4 should still be there", '@', testAdapter.elementOnPosition(11, 4));
		
		// test if nothing changed after end turn
		testAdapter.endTurn();
		
		// the other player can't "attack"/walk, too
		testAdapter.changeActivePlayer(11, 4);
		testAdapter.performActionForActivePlayer();
		assertEquals("Player at 11:4 should still have 75 actionpoints", 75, testAdapter.actionPointsLeft());
		assertEquals("Player at 11:3 should still be there", '@', testAdapter.elementOnPosition(11, 3));
		assertEquals("Player at 11:4 should still be there", '@', testAdapter.elementOnPosition(11, 4));
	}
}
